﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace PeisongWeb.admin
{
    public partial class ReturnProduct_Processing : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DG", 1))
                {
                   
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }


        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            string orderno = this.txt_OrderNo.Text.Trim();
            if (logic.returnProduct.P_Order_getByOrderNo(orderno) > 0)
            {
                switch (logic.returnProduct.P_Order_ReturnProduct(orderno))
                {
                    case 1:
                        Library.Script.ClientMsg("该订单已生成应收款账单，不能进行退货");
                        this.admin_list_content.Visible = false;
                        break;//代表不能退货
                    case 2:
                        Library.Script.ClientMsg("订单状态不是交易成功状态，不能进行退货");
                        this.admin_list_content.Visible = false;
                        break;//代表退货要求订单状态为交易成功或交易关闭
                    case 0:
                        this.admin_list_content.Visible = true;
                        GetRepeaterOrderItem(orderno);
                        break;//可以退货
                }
            }
            else
            {
                Library.Script.ClientMsg("订单编号不存在,请重新输入");
            }
        }

        void GetRepeaterOrderItem(string OrderNo)
        {
            rpt_OrderItems.DataSource = logic.orderItem.select("orderNo=" + OrderNo, "oiId", 100, 1).Tables[1];
            rpt_OrderItems.DataBind();
        }
        protected void rpt_OrderItems_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                if (e.CommandName == "Del")
                {
                    
                    this.hdoiid.Value = e.CommandArgument.ToString().Split(',')[0];
                    this.hdquantity.Value = e.CommandArgument.ToString().Split(',')[1];
                    int oiid = 0;
                    int.TryParse(hdoiid.Value, out oiid);
                    if (IsByProcessing(oiid, 0, string.Empty, 0) == 0)
                    {
                        Library.Script.ClientMsg("该订单已生成应付款，不能进行退货");
                    }
                    else
                    {
                        this.txtQuantity.Text = this.hdquantity.Value;
                        this.win_Deduct.Show();
                    }
                }
            }
        }

        /// <summary>
        /// 退货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            int adminid = logic.sysAdmin.AdminID;
            int oiid = 0;
            int.TryParse(hdoiid.Value,out oiid);
            string notes = this.txtNotes.Text;
           
            decimal quantity = 0;
            try
            {
                quantity = Convert.ToDecimal(this.txtQuantity.Text);
            }
            catch { quantity = 0; }
            if (quantity>(decimal)0 && quantity <= Convert.ToDecimal(this.hdquantity.Value))
            {
                if (IsByProcessing(oiid, adminid, notes, quantity) == 1)
                {
                    Library.Script.ClientMsgUrl("退货已成功", "ReturnProduct_Processing.aspx");
                }
            }
            else
            {
                Library.Script.ClientMsg("输入的退货的数量不正确，请重新输入");
            }
        }

        int IsByProcessing(int oiid, int adminid,string snote,decimal quantity)
        {
            return logic.returnProduct.P_order_returnPros_IsByProcessing(oiid, adminid, snote,quantity);
        }
    }


}
